home *** CD-ROM | disk | FTP | other *** search
- /*
- File: ThreadContext.cp
-
- Contains: An abstract class for a thread context
-
- Written by: Steve Sisak
-
- Copyright: © 1995 by Steve Sisak, all rights reserved.
-
- Change History (most recent first):
-
- */
-
- #ifndef _THREADCONTEXT_
- #include "ThreadContext.h"
- #endif
-
- #include "Exceptions.h"
-
- TThreadContext::TThreadContext()
- {
- fThreadID = kNoThreadID;
- fThreadResult = nil;
- #ifdef __MWERKS__
- // __new_exception_state(&fExceptionState,fCatchBuffer); // Use for CW 6
- __new_exception_state(&fExceptionState,fCatchBuffer,sizeof(fCatchBuffer)); // Use for CW 7
- #endif
- }
-
-
- TThreadContext::~TThreadContext()
- {
- }
-
-
- void TThreadContext::CreateThread(
- ThreadStyle threadStyle,
- ThreadEntryProcPtr threadEntry,
- Size stackSize,
- ThreadOptions options)
- {
- FailOSErr(NewThread(threadStyle, threadEntry, this, stackSize, options, &fThreadResult, &fThreadID));
-
- Try
- {
- FailOSErr(SetThreadSwitcher(fThreadID, SwitchInProc, this, true));
- FailOSErr(SetThreadSwitcher(fThreadID, SwitchOutProc, this, false));
- FailOSErr(SetThreadTerminator(fThreadID, TerminationProc, this));
- }
- Catch(err)
- {
- OSErr ignore = DisposeThread(fThreadID, &fThreadResult, (options & kUsePremadeThread) != 0);
-
- fThreadID = kNoThreadID;
-
- Throw(err);
- }
- }
-
-
- void TThreadContext::SwitchIn(void)
- {
- #ifdef __MWERKS__
- __switch_exception_state(&fExceptionState, &fSavedState);
- #endif
- }
-
-
- void TThreadContext::SwitchOut(void)
- {
- #ifdef __MWERKS__
- __switch_exception_state(&fSavedState, &fExceptionState);
- #endif
- }
-
-
- void TThreadContext::Terminate(void)
- {
- if (fThreadID != kNoThreadID)
- {
- fThreadID = kNoThreadID;
- }
- }
-
-
- pascal void TThreadContext::SwitchInProc(ThreadID /*threadBeingSwitched*/, void *switchProcParam)
- {
- TThreadContext* thread = (TThreadContext*) switchProcParam;
-
- thread->SwitchIn();
- }
-
-
- pascal void TThreadContext::SwitchOutProc(ThreadID /*threadBeingSwitched*/, void *switchProcParam)
- {
- TThreadContext* thread = (TThreadContext*) switchProcParam;
-
- thread->SwitchOut();
- }
-
-
- pascal void TThreadContext::TerminationProc(ThreadID /*threadBeingSwitched*/, void *terminationProcParam)
- {
- TThreadContext* thread = (TThreadContext*) terminationProcParam;
-
- thread->Terminate();
- }
-
-
-